﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// 列举出现有的相关的发货单
    /// 新建发货单
    /// 将销售单行加入现在的发货单
    /// 新建发货单必须选择其中一行，建立VVT和VV
    /// WZ09留位，取消留位，通知WV03
    ///WV03，确定分批，取消分批，取消留位，通知WZ09
    /// </summary>
    public partial class JLTWV04 
    {
        #region ==IsWBFull===========
        public static readonly DependencyProperty IsWBFullProperty = DependencyProperty.Register("IsWBFull",
            typeof(bool), typeof(JLTWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsWBFullChanged));
        public bool IsWBFull
        {
            get
            {
                return (bool)GetValue(IsWBFullProperty);
            }
            set
            {
                SetValue(IsWBFullProperty, value);
                OnIsWBFullChg(value);
            }
        }
        private static void OnIsWBFullChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTWV04;
            if (src != null)
            {
                src.IsWBFull = (bool)e.NewValue;
            }
        }
        #endregion
        public void OnIsWBFullChg(bool value)
        {
            if (value)
            {

                ZoneVK.Visibility = Visibility.Collapsed;
                ZoneWB.SetValue(Grid.RowProperty, 1);
                ZoneWB.SetValue(Grid.RowSpanProperty, 2);
            }
            else
            {
                ZoneVK.Visibility = Visibility.Visible;
                ZoneWB.SetValue(Grid.RowProperty, 2);
                ZoneWB.SetValue(Grid.RowSpanProperty, 1);
            }
        }
        #region ==CheckCPCW===========
        public static readonly DependencyProperty CheckCPCWProperty = DependencyProperty.Register("CheckCPCW",
            typeof(bool), typeof(JLTWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckCPCWChanged));
        public bool CheckCPCW
        {
            get
            {
                return (bool)GetValue(CheckCPCWProperty);
            }
            set
            {
                SetValue(CheckCPCWProperty, value);
                if (value)
                { 
                    CheckCP = false;
                    CheckCW = false;
                }
            }
        }
        private static void OnCheckCPCWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTWV04;
            if (src != null)
            {
                src.CheckCPCW = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CheckCP===========
        public static readonly DependencyProperty CheckCPProperty = DependencyProperty.Register("CheckCP",
            typeof(bool), typeof(JLTWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckCPChanged));
        public bool CheckCP
        {
            get
            {
                return (bool)GetValue(CheckCPProperty);
            }
            set
            {
                SetValue(CheckCPProperty, value);
                if (value)
                {
                    CheckCPK = false;
                    CheckCW = false;
                    CheckCPCW = false;
                }
            }
        }
        private static void OnCheckCPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTWV04;
            if (src != null)
            {
                src.CheckCP = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CheckCW===========
        public static readonly DependencyProperty CheckCWProperty = DependencyProperty.Register("CheckCW",
            typeof(bool), typeof(JLTWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckCWChanged));
        public bool CheckCW
        {
            get
            {
                return (bool)GetValue(CheckCWProperty);
            }
            set
            {
                SetValue(CheckCWProperty, value);
                if (value)
                {
                    CheckCPK = false;
                    CheckCP = false;
                    CheckCPCW = false;
                }
            }
        }
        private static void OnCheckCWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTWV04;
            if (src != null)
            {
                src.CheckCW = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CheckCPK===========
        public static readonly DependencyProperty CheckCPKProperty = DependencyProperty.Register("CheckCPK",
            typeof(bool), typeof(JLTWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckCPKChanged));
        public bool CheckCPK
        {
            get
            {
                return (bool)GetValue(CheckCPKProperty);
            }
            set
            {
                SetValue(CheckCPKProperty, value);
                if (value)
                {
                    CheckCPCW = false;
                    CheckCP = false;
                    CheckCW = false;
                }
            }
        }
        private static void OnCheckCPKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTWV04;
            if (src != null)
            {
                src.CheckCPK = (bool)e.NewValue;
            }
        }
        #endregion

       
        ITXEXRPT ITXEXRPT;
        private ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        VJLTWV04 vm;
        public JLTWV04()
        { 
            vm = new VJLTWV04();
            DataContext = vm;
            InitializeComponent();
            this.Name = "发货组织";
            if (ITXEXRPT == null)
            {
                ITXEXRPT = new ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
            }
              if (app.User.Trim() == "JLTV")
            {
                cVK04.KRS = "J3";
                cVK04.LFD = "002008"; 
            }
             cVK05.IsNVV = true;
             cWZ09.LO = "01";
             cWZ09.LB = "CP";
             cVK05.ARTST = "F";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
        }


        VWBBST wb;
        private void btnPrintPK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ITXEXRPT.dsSource = new DataSet();
                ITXEXRPT.DocType = "JLTVV03PKCN";
                if (txtLang.Text == "EN")
                {
                    ITXEXRPT.DocType = "JLTVV03PKEN";
                }
                ITXEXRPT.dsSource.Merge(cVVT.MV.SetProd);
                ITXEXRPT.dsSource.Merge(this.cVK05.MV.SetProd);
                if(wb==null) wb=new VWBBST();
                DataRow dwData = wb.TBMain.NewRow();
                dwData["WBBMANDANT"]=app.Mandant;
                dwData["WBBAUFKRS"] = cWZ09.KRS;
                dwData["WBBAUFLFD"] = cWZ09.LFD;
                dwData["WBBAUFPOS"] = cWZ09.POS;
                dwData["WBBALIEFSCH"] = cWZ09.LIEFSCH;

                DataSet SResult = wb.QueryWBBST(dwData, 17);
                string filter = "";
                DataRow[] rows = null;
                DataTable tb;
                if (CheckCW)
                {
                    filter = "WBBLB='CW'";
                    rows = SResult.Tables[0].Select(filter);
                    if (rows.Length == 0)
                        return;
                    tb = rows.CopyToDataTable();
                    tb.TableName = "WBBST";

                    ITXEXRPT.dsSource.Merge(tb);
                    SResult = ((VVKPOS)cVK05.MV).QueryVKP(cVK05.dwVKP);
                    ITXEXRPT.dsSource.Merge(SResult);
                    ITXEXRPT.strExcelFile = @"\JLT\JLTVVDOC.xls";
                    string filename = ITXEXRPT.WriteExcelReport();
                    return;
                }
                if (CheckCPK)
                {
                    filter = "WBBLB='CW' OR WBBLB='CP' OR WBBLB='KO' ";
                    rows = SResult.Tables[0].Select(filter);
                    if (rows.Length == 0)
                        return;
                    tb = rows.CopyToDataTable();
                    tb.TableName = "WBBST";

                    ITXEXRPT.dsSource.Merge(tb);
                    SResult = ((VVKPOS)cVK05.MV).QueryVKP(cVK05.dwVKP);
                    ITXEXRPT.dsSource.Merge(SResult);
                    ITXEXRPT.strExcelFile = @"\JLT\JLTVVDOC.xls";
                    string filename = ITXEXRPT.WriteExcelReport();
                    return;
                }
                if (CheckCPCW)
                {
                    filter = "WBBLB='CW' OR WBBLB='CP' ";
                    rows = SResult.Tables[0].Select(filter);
                    if (rows.Length == 0)
                        return;
                    tb = rows.CopyToDataTable();
                    tb.TableName = "WBBST";

                    ITXEXRPT.dsSource.Merge(tb);
                    SResult = ((VVKPOS)cVK05.MV).QueryVKP(cVK05.dwVKP);
                    ITXEXRPT.dsSource.Merge(SResult);
                    ITXEXRPT.strExcelFile = @"\JLT\JLTVVDOC.xls";
                    string filename = ITXEXRPT.WriteExcelReport();
                    return;
                }
                if (CheckCP)
                {
                    filter = "WBBLB='CP'";
                    rows = SResult.Tables[0].Select(filter);
                    if (rows.Length == 0)
                        return;
                    tb = rows.CopyToDataTable();
                    tb.TableName = "WBBST";

                    ITXEXRPT.dsSource.Merge(tb);
                    SResult = ((VVKPOS)cVK05.MV).QueryVKP(cVK05.dwVKP);
                    ITXEXRPT.dsSource.Merge(SResult);
                    ITXEXRPT.strExcelFile = @"\JLT\JLTVVDOC.xls";
                    string filename = ITXEXRPT.WriteExcelReport();
                    return;
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
}
